const MYSQL = require("../../sql/mysql");
// MYSQL数据库配置信息
const MYSQLConfig = require("../../sql/database");
let sqlName = MYSQLConfig.datasheetName.roles;
/**
 * 角色是否存在（根据权限查找）
 * @param {object} params
 * @param {string} params.scope 权限标识名称
 * @returns {Promise<{code:string,msg:string,exist:boolean}>}
 */
module.exports = async (params = {}) => {
  try {
    let { scope } = params;
    if (!scope) {
      throw { code: 500, msg: "scope不能为空" };
    }
    let query = `SELECT * FROM ${sqlName} WHERE scope = '${scope}'`;
    let roles = null;
    try {
      roles = await MYSQL.queryPromise(query);
    } catch (err) {
      throw {
        code: 200,
        msg: "查询失败，数据库操作不成功",
      };
    }
    if (roles.length) {
      return {
        code: 0,
        msg: "权限存在",
        exist: true,
      };
    } else {
      return {
        code: 0,
        msg: "权限不存在",
        exist: false,
      };
    }
  } catch (error) {
    throw error;
  }
};
